package cn.iocoder.yudao.module.system.controller.admin.workstorepallet;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.workstorepallet.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workstorepallet.WorkStorePalletDO;
import cn.iocoder.yudao.module.system.service.workstorepallet.WorkStorePalletService;

@Tag(name = "管理后台 - 入库托数记录")
@RestController
@RequestMapping("/system/work-store-pallet")
@Validated
public class WorkStorePalletController {

    @Resource
    private WorkStorePalletService workStorePalletService;

    @PostMapping("/create")
    @Operation(summary = "创建入库托数记录")
    @PreAuthorize("@ss.hasPermission('system:work-store-pallet:create')")
    public CommonResult<Long> createWorkStorePallet(@Valid @RequestBody WorkStorePalletSaveReqVO createReqVO) {
        return success(workStorePalletService.createWorkStorePallet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新入库托数记录")
    @PreAuthorize("@ss.hasPermission('system:work-store-pallet:update')")
    public CommonResult<Boolean> updateWorkStorePallet(@Valid @RequestBody WorkStorePalletSaveReqVO updateReqVO) {
        workStorePalletService.updateWorkStorePallet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除入库托数记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-store-pallet:delete')")
    public CommonResult<Boolean> deleteWorkStorePallet(@RequestParam("id") Long id) {
        workStorePalletService.deleteWorkStorePallet(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除入库托数记录")
                @PreAuthorize("@ss.hasPermission('system:work-store-pallet:delete')")
    public CommonResult<Boolean> deleteWorkStorePalletList(@RequestParam("ids") List<Long> ids) {
        workStorePalletService.deleteWorkStorePalletListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得入库托数记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-store-pallet:query')")
    public CommonResult<WorkStorePalletRespVO> getWorkStorePallet(@RequestParam("id") Long id) {
        WorkStorePalletDO workStorePallet = workStorePalletService.getWorkStorePallet(id);
        return success(BeanUtils.toBean(workStorePallet, WorkStorePalletRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得入库托数记录分页")
    @PreAuthorize("@ss.hasPermission('system:work-store-pallet:query')")
    public CommonResult<PageResult<WorkStorePalletRespVO>> getWorkStorePalletPage(@Valid WorkStorePalletPageReqVO pageReqVO) {
        PageResult<WorkStorePalletDO> pageResult = workStorePalletService.getWorkStorePalletPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkStorePalletRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出入库托数记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-store-pallet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkStorePalletExcel(@Valid WorkStorePalletPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkStorePalletDO> list = workStorePalletService.getWorkStorePalletPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "入库托数记录.xls", "数据", WorkStorePalletRespVO.class,
                        BeanUtils.toBean(list, WorkStorePalletRespVO.class));
    }

}